#ifndef _tenacitas__balance_line__test2_h_
#define _tenacitas__balance_line__test2_h_


#include <list>
#include <cstdint>
#include <memory>
#include <algorithm>
#include <string>
#include <iostream>
#include <fstream>
#include <chrono>
#include <thread>

#include <tenacitas.balance_line/merge_all.h>
#include <tenacitas.log/text_file.h>
#include <tenacitas.exception/exception.h>

#include <boost/filesystem.hpp>

using namespace boost;

#ifndef DIR_OUTPUT
#define DIR_OUTPUT "./"
#endif

namespace tenacitas {
    /** namespace for the project */
    namespace balance_line {
        namespace test2 {


            typedef tenacitas::log::text_file logger;
            typedef tenacitas::log::text_line text_line;
            
            
            /** */
            struct log_file {

                typedef std::shared_ptr<log_file> ptr;
                typedef text_line data;
                typedef text_line::ptr data_ptr;


                log_file( const std::string & p_file_name )
                    :m_file( p_file_name.c_str( ) ),
                     m_line( text_line::create( ) ),
                     m_name( p_file_name ) { }
                             

                data_ptr read() {
                    data_ptr l_rc;
                    
                    if ( m_line->read( m_file ) ) {
                        if ( m_line->is_header( ) ) {
                            std::cout << "line '" << *m_line << "' is a header line" << std::endl;
                            l_rc = read( );
                        }
                        else {
                            std::cout << "line '" << *m_line << "' is NOT a header line" << std::endl;
                            l_rc = m_line;
                        }
                    }
                    return l_rc;
                }

                const std::string & name( ) const {
                    return m_name;
                }


            private:
                std::ifstream m_file;
                data_ptr m_line;
                std::string m_name;
            };

            /** */
            struct output {
                output( )
                    : m_file( std::string( DIR_OUTPUT ) +
                              std::string( "/output.txt" ), std::ios::trunc ) {
                    
                    if ( !m_file.is_open( ) ) {
                        tenacitas::exception::exception l_ex =
                            make_tenacitas_exception( "Unable to open file 'output.log'") ;
                        
                        throw( l_ex );
                    }
                            
                    m_file << text_line::get_header( ) << std::endl;
                }

                ~output( ) {
                    m_file.close( );
                }
                
                
                void add( text_line::ptr p_line ) {
                    if ( !p_line->is_header( ) ) {
                        m_file << *p_line << std::endl;
                    }
                }


            private:
                std::ofstream m_file;                
            };

            
            /** runs the test */
            void run( );

            void f0( );

            void f1( );            

            void f2( std::int16_t p_repeats,
                     std::int16_t p_stop_at,
                     std::int32_t p_sleep,
                     const std::string p_prefix );
            

        }
    }
}

#endif
